home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / kpini / inifile.bas < prev   
BASIC Source File  |  1995-05-09  |  32KB  |  1,076 lines

  1. '***************************************************************************
  2. '** INIFILE.BAS ** Third Public Release
  3. '*************************************************
  4. '** VB Module for simplifying .INI file operations
  5. '***************************************************************************
  6. 'Written and modified by Karl E. Peterson, February 1994, CIS 72302,3707.
  7. 'Portions originally downloaded from CompuServe's MSBASIC forum as
  8. 'MINIFILE.BAS, author unknown.  Comments and questions welcome!
  9. '***************************************************************************
  10. 'This module contains "wrappers" for just about anything you'd want to do
  11. 'with INI files.  The only prerequisite for using them is to register the
  12. 'particular INI path/filename and [Section] in advance of calling them.
  13. 'Register Private.Ini by calling PrivIniRegister, and Win.Ini by calling
  14. 'WinIniRegister.
  15. '
  16. 'This provides *safe* assured access to both application (Private.Ini) and
  17. 'Windows (Win.Ini) initialization files, with no need to worry about proper
  18. 'declarations and calling conventions.  It also greatly simplifies the task
  19. 'of repeatedly reading or writing to an Ini file.
  20. '
  21. 'You are free to use this module as you see fit.  If you like it, I'd really
  22. 'appreciate hearing that!  If you don't like it, or have problems with it,
  23. 'I'd like to know that too.
  24. '***************************************************************************
  25. 'The SECOND RELEASE added a dozen new functions, and two old ones were renamed.
  26. 'Latest modifications, June 1994
  27. '  WinGetSectionEntries() is now WinGetSectEntries()
  28. '  PrivGetSectionEntries() is now PrivGetSectEntries()
  29. 'Two new functions retrieve an entire [Section], entries and values, into an
  30. 'array from either Win.Ini or Private.Ini.  These functions are:
  31. '  WinGetSectEntriesEx()
  32. '  PrivGetSectEntriesEx()
  33. 'The other four deal with problems associated with multiple "device=" lines
  34. 'in System.Ini.  Use these at your *own risk*!  Especially the ones that add
  35. 'or remove a device.  These functions are:
  36. '  SysDevAdd()              Adds a "device=" line to System.Ini
  37. '  SysDevRemove()           Removes a "device=" line from System.Ini
  38. '  SysDevLoaded()           Checks for a specific "device=" line
  39. '  SysDevGetList()          Retrieves array of all devices
  40. 'The last six deal with [Section]'s.
  41. '  Win/PrivGetSections()    Retrieves list of all [Section]'s
  42. '  Win/PrivGetSectionsEx()  Retrieves array of all [Section]'s
  43. '  Win/PrivSectionExist()   Verifies existence of registered [Section]
  44. '***************************************************************************
  45. 'This THIRD RELEASE fixes a problem with the SysDevLoaded and SysDevRemove
  46. 'functions.  Neither worked if comments were on the same line.  Also, a flag
  47. 'has been added so that paths can be ignored or enforced with the SysXXX
  48. 'functions.  All API calls have been Aliased, so that this module may more
  49. 'easily be incorporated into existing programs.  Four new routines have
  50. 'been added:
  51. '  SysIniRegister()         Set nmSysPath flag
  52. '  ExtractName$()           Returns filename from filespec
  53. '  ExtractPath$()           Returns path from filespec
  54. '  StripComment$()          Removes trailing comments/spaces
  55. '***************************************************************************
  56.  
  57. Option Explicit
  58.  
  59. '** Windows API calls
  60. '(NOTE: Profile calls *altered* from those found in WIN30API.TXT!)
  61.   Declare Function kpGetProfileInt Lib "Kernel" Alias "GetProfileInt" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Integer) As Integer
  62.   Declare Function kpGetProfileString Lib "Kernel" Alias "GetProfileString" (ByVal lpAppName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer) As Integer
  63.   Declare Function kpWriteProfileString Lib "Kernel" Alias "WriteProfileString" (ByVal lpAppName As Any, ByVal lpKeyName As Any, ByVal lpString As Any) As Integer
  64.   Declare Function kpGetPrivateProfileInt Lib "Kernel" Alias "GetPrivateProfileInt" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
  65.   Declare Function kpGetPrivateProfileString Lib "Kernel" Alias "GetPrivateProfileString" (ByVal lpAppName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  66.   Declare Function kpWritePrivateProfileString Lib "Kernel" Alias "WritePrivateProfileString" (ByVal lpAppName As Any, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Integer
  67.   Declare Function kpSendMessage Lib "User" Alias "SendMessage" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
  68.   Declare Function kpGetWindowsDirectory Lib "Kernel" Alias "GetWindowsDirectory" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
  69.  
  70. '** Module-level variables for [Section] and Ini file names
  71.   Dim smSectionName As String   'Current section in private Ini file
  72.   Dim smIniFileName As String   'Fully qualified path/name of current private Ini file
  73.   Dim smWinSection As String    'Current section in Win.Ini
  74.   Dim nmWinInit As Integer      'Flag to indicate that Win.Ini section is initialized
  75.   Dim nmPrivInit As Integer     'Flag to indicate that Private.Ini is initialized
  76.   Dim nmSysPath As Integer      'Flag to indicate whether paths should be used with DEVICE=
  77.  
  78. '** Constants used to size buffers
  79.   Const Max_SectionBuffer = 4096
  80.   Const Max_EntryBuffer = 255
  81.  
  82. '** Special values to alert other apps of Win.Ini changes
  83.   Const HWND_BROADCAST = &HFFFF
  84.   Const WM_WININICHANGE = &H1A
  85.  
  86. Function ExtractName$ (sSpecIn$, nBaseOnly%)
  87.   
  88.   Dim nCnt%, nDot%, sSpecOut$
  89.  
  90.   On Local Error Resume Next
  91.  
  92.   If InStr(sSpecIn, "\") Then
  93.     For nCnt = Len(sSpecIn) To 1 Step -1
  94.       If Mid$(sSpecIn, nCnt, 1) = "\" Then
  95.         sSpecOut = Mid$(sSpecIn, nCnt + 1)
  96.         Exit For
  97.       End If
  98.     Next nCnt
  99.   
  100.   ElseIf InStr(sSpecIn, ":") = 2 Then
  101.     sSpecOut = Mid$(sSpecIn, 3)
  102.     
  103.   Else
  104.     sSpecOut = sSpecIn
  105.   End If
  106.     
  107.   If nBaseOnly Then
  108.     nDot = InStr(sSpecOut, ".")
  109.     If nDot Then
  110.       sSpecOut = Left$(sSpecOut, nDot - 1)
  111.     End If
  112.   End If
  113.  
  114.   ExtractName$ = UCase$(sSpecOut)
  115.  
  116. End Function
  117.  
  118. Function ExtractPath$ (sSpecIn$)
  119.  
  120.   Dim nCnt%, sSpecOut$
  121.   
  122.   On Local Error Resume Next
  123.  
  124.   If InStr(sSpecIn, "\") Then
  125.     For nCnt = Len(sSpecIn) To 1 Step -1
  126.       If Mid$(sSpecIn, nCnt, 1) = "\" Then
  127.         sSpecOut = Left$(sSpecIn, nCnt)
  128.         Exit For
  129.       End If
  130.     Next nCnt
  131.   
  132.   ElseIf InStr(sSpecIn, ":") = 2 Then
  133.     sSpecOut = CurDir$(sSpecIn)
  134.     If Len(sSpecOut) = 0 Then sSpecOut = CurDir$
  135.  
  136.   Else
  137.     sSpecOut = CurDir$
  138.   End If
  139.     
  140.   If Right$(sSpecOut, 1) <> "\" Then
  141.     sSpecOut = sSpecOut + "\"
  142.   End If
  143.   ExtractPath$ = UCase$(sSpecOut)
  144.  
  145. End Function
  146.  
  147. Sub Main ()
  148.   'This subroutine is useful for simply testing the other routines in this
  149.   'module.  Make this module the only one in a project, and set Sub Main as
  150.   'the entry point.  Then enter the code you wish to test below.
  151. End Sub
  152.  
  153. Sub PrivClearEntry (sEntryName As String)
  154.  
  155.   'Bail if not initialized
  156.     If Not nmPrivInit Then
  157.       PrivIniNotReg
  158.       Exit Sub
  159.     End If
  160.  
  161.   'Sets a specific entry in Private.Ini to Nothing or Blank
  162.     Dim nRetVal As Integer
  163.     nRetVal = kpWritePrivateProfileString(smSectionName, sEntryName, "", smIniFileName)
  164.  
  165. End Sub
  166.  
  167. Sub PrivDeleteEntry (sEntryName As String)
  168.  
  169.   'Bail if not initialized
  170.     If Not nmPrivInit Then
  171.       PrivIniNotReg
  172.       Exit Sub
  173.     End If
  174.  
  175.   'Deletes a specific entry in Private.Ini
  176.     Dim nRetVal As Integer
  177.     nRetVal = kpWritePrivateProfileString(smSectionName, sEntryName, 0&, smIniFileName)
  178.  
  179. End Sub
  180.  
  181. Sub PrivDeleteSection ()
  182.  
  183.   'Bail if not initialized
  184.     If Not nmPrivInit Then
  185.       PrivIniNotReg
  186.       Exit Sub
  187.     End If
  188.  
  189.   'Deletes an *entire* [Section] and all its Entries in Private.Ini
  190.     Dim nRetVal As Integer
  191.     nRetVal = kpWritePriv